package com.huo.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.huo.dao.PermissionDao;
import com.huo.dao.RoleDao;
import com.huo.dao.UserDao;
import com.huo.pojo.Permission;
import com.huo.pojo.Role;
import com.huo.pojo.User;
import com.huo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Set;

@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private RoleDao roleDao;

    @Autowired
    private PermissionDao permissionDao;

    /**
     * @Description: 根据用户名查询用户
     * @Author: 汪青松
     * @Param: [username]
     * @Return: com.huo.pojo.User
     * @CreateDate: 2021/2/27 23:30
     */
    @Override
    public User findByUserName(String username) {
        User user = userDao.findByUserName(username);

        if (user == null) {
            return null;
        }

        Integer userId = user.getId();
        Set<Role> roles = roleDao.findRolesByUserId(userId);

        if (roles != null) {
            for (Role role : roles) {
                Integer roleId = role.getId();
                Set<Permission> permissions = permissionDao.findPermissionsByRoleId(roleId);
                role.setPermissions(permissions);
            }
        }
        user.setRoles(roles);
        return user;
    }
}
